package com.lyf.scm.admin.api.controller;

import com.lyf.scm.admin.api.dto.DirectlyStoreDTO;
import com.lyf.scm.admin.domain.service.DirectlyStoreService;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.model.ExcelExportDataDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * description:
 *          直营预测计划--门店Controller
 * @author: haodong
 * @data: 2020/8/29 17:16
 */
@Slf4j
@Api(tags = "直营预测计划--执行")
@RequestMapping("/api/v1/salesPlanDirectlyStore")
@RomeController
public class DirectlyStoreController {

    @Resource
    private DirectlyStoreService directlyStoreService;

    @PostMapping("/list")
    @ApiOperation(value = "按条件查询直营预测计划(天维度)")
    public Response listBySelective(@RequestBody DirectlyStoreDTO directlyStore) {
        return Response.builderSuccess(directlyStoreService.listBySelective(directlyStore));
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "查询全渠道直营预测计划(天维度)")
    public Response listAllByDays(@RequestBody DirectlyStoreDTO directlyStore) {
        return Response.builderSuccess(directlyStoreService.listAllByDays(directlyStore));
    }

    @PostMapping("/updateConfirm")
    @ApiOperation(value = "更新确认量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unitType", value = "单位类型", required = true, paramType = "query", dataType = "long"),
    })
    public Response updateConfirm(@RequestBody List<DirectlyStoreDTO> directlyStoreList, @RequestParam Long unitType) {
        return Response.builderSuccess(directlyStoreService.updateConfirm(directlyStoreList, unitType));
    }

    @PostMapping("/exportExcel")
    @ApiOperation("导出Excel")
    public void exportExcel(@RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse) {
        try {
            //获得feign响应流对象
            ExcelExportDataDTO excelExportData = directlyStoreService.exportExcel(excelExportDTO, type);
            String sourceFileName = "直营(门店)预测计划";
            ExcelExportUtil.exportExcel(httpServletResponse, excelExportData,sourceFileName);
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }
}
